package com.shanjiduo.newsline.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shanjiduo.newsline.R;
import com.shanjiduo.newsline.model.MovieSummary;
import com.shanjiduo.newsline.model.NewsSummary;

import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by Administrator on 2017/1/13.
 */

public class MovieAdapter extends BaseAdapter {
    private Activity activity;
    private List<MovieSummary> list;
    private DisplayImageOptions options;

    public MovieAdapter(Activity activity) {
        this.activity = activity;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.small_image_holder_listpage_loading)
                .showImageForEmptyUri(R.drawable.small_loadpic_empty_listpage)
                .showImageOnFail(R.drawable.small_image_holder_listpage_failed)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
    }

    public void setData(List<MovieSummary> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public MovieSummary getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = View.inflate(activity, R.layout.movie_fragment_layout, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        MovieSummary summary = getItem(i);
        holder.playerNum.setText(summary.ptime);
        holder.source.setText(summary.videosource);
        int time = summary.length;
        int mine = time / 60;
        int s = time % 60;
        holder.time.setText(mine + "分" + s + "秒");
        //  ImageLoader.getInstance().displayImage(summary.cover,holder.movieImg,options);
        //  JCVideoPlayerStandard jcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.custom_videoplayer_standard);

        holder.movieImg.setUp(summary.mp4_url, summary.title);
        holder.movieImg.ivThumb.setScaleType(ImageView.ScaleType.FIT_XY);
        ImageLoader.getInstance().displayImage(summary.cover, holder.movieImg.ivThumb, options);

        //  jcVideoPlayerStandard.ivThumb.setThumbInCustomProject("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");
        ImageLoader.getInstance().displayImage(summary.topicImg, holder.authorImg, options);
        JCVideoPlayer.releaseAllVideos();
        return view;
    }

    private class ViewHolder {

        public TextView source;
        public TextView playerNum;
        public TextView time;
        public JCVideoPlayerStandard movieImg;
        public ImageView authorImg;

        public ViewHolder(View view) {
            source = (TextView) view.findViewById(R.id.movie_fragment_source);
            playerNum = (TextView) view.findViewById(R.id.movie_fragment_infor);
            time = (TextView) view.findViewById(R.id.movie_fragment_time);
            movieImg = (JCVideoPlayerStandard) view.findViewById(R.id.custom_videoplayer_standard);
            authorImg = (ImageView) view.findViewById(R.id.movie_fragment_person_image);

        }


    }
}
